program TASACS
! File:     TASACS.f90
! Purpose:  This file contains the main program of HModel which sets up and
!           governs the selection between a defined set of subprograms
! History : When        Who        What
!           16-04-07    Skalevik   Created
   use Optimisation
   implicit none
   type(TOptimisation) :: Opt
   write(*,*) "-------------------"
   write(*,*) "---  TASACS 1.1  --"
   write(*,*) "-------------------"

   ! Specify run directory:
   Opt%DumpCalculationDetails = .false.
   
   Opt%RunName = "Working run"	

   !Opt%ProjectDir = "C:\Projects\tasacs\Data\Clean"
   !Opt%ProjectDir = "C:\Projects\tasacs\Data\Saithe 2008"	
   !Opt%ProjectDir = "C:\Projects\tasacs\Data\ISSH"	
   Opt%ProjectDir = "C:\Projects\tasacs\Data\NSSH 2008\NSSH Final"	
   !Opt%ProjectDir = "C:\Projects\tasacs\Data\temp\test"
   !Opt%ProjectDir = "C:\projects\TASACS\Data\BW 2008"	
   if(.not. LoadRun(Opt)) then
      write(*,*) lrtrim(Opt%ErrorStr)
      write(*,*) "The program has been stopped."
      write(*,*) "Fix the errors and retry!"
      stop
   endif
   
   call DoRun(Opt)
   call CloseRun(Opt)
   
   !Opt%ProjectDir = "C:\Projects\tasacs\Data\Pakke\clean"
   !call DoTestingOfAllRuns(Opt)

   !Opt%ProjectDir = "C:\Projects\tasacs\Data\Clean0"
   !call DoTestingOfAllRuns(Opt)
   
end program TASACS


subroutine DoRun(Opt)
   use Optimisation
   implicit none
   type(TOptimisation) :: Opt
   ! Load the run from file

   if(Opt%RunErrorStr /= "") then
      write(*,*) "Run time error!"
      write(*,*) lrtrim(Opt%RunErrorStr)
      return
   endif

   !call WriteObsData(Opt)
   ! The tuning loop (tuning driver) with specific output
   do
      ! Perform tuning (finish if return value is true):
      if (RunOpt(Opt)) exit
      ! User output:
      write(*,*) "Stage: ", Opt%Loc%Stage, "Iter: ",  Opt%Loc%Iter, &
      ", Delta: ", lrtrim(floattostrex(Opt%Loc%Delta, 5)), &
      ", ObjFunc: ", lrtrim(floattostrex(Opt%Loc%ObjFunc, 5))
   enddo
end subroutine DoRun


subroutine DoTestingOfAllRuns(Opt)
   use Optimisation
   implicit none
   type(TOptimisation) :: Opt
   character(len = 256) :: FileName
   integer :: Model, PlusGroup, ISVPAType, ObjT, ioerr, DataSourceType, &
      NumDataSources, iDataSource, iDataSourceType, MaxISVPAType, MinFlag, AFlag, iYear
   type(TDataSource), pointer :: pDataSource
   type(TParamElm), pointer :: pParamElm
   integer :: MinModel, MaxModel, MinPlusGroup, MaxPlusGroup, MinObjType, MaxObjType 


   FileName = GetWorkFileName(Opt, "OverallTest.txt")
   open(cFileUnit, file = FileName, action = 'write', status='replace')
   close(cFileUnit);

   MinModel = 0
   MaxModel = 0
   MinPlusGroup = 1
   MaxPlusGroup = 1
   MinObjType = 5
   MaxObjType = 5 
   
   do Model = MinModel, MaxModel
      select case(Model)
         case(2)
            MaxISVPAType = 1
         case default
            MaxISVPAType = 0
      end select
      select case(Model)
         case(1)
            MinFlag = 0
         case default
            MinFlag = 1
      end select
      do ISVPAType = 0, MaxISVPAType
         do PlusGroup = MinPlusGroup, MaxPlusGroup
            do ObjT = MinObjType, MaxObjType
               do AFlag = MinFlag, 1
                  if(.not. LoadRun(Opt)) exit
                  Opt%IndexInput%ISVPAControlType = ISVPAType
                  Opt%IndexInput%PlusGroupMethod = PlusGroup
                  Opt%IndexInput%ModelType = Model
                  Opt%IndexInput%ProcessType = ptOrdinaryAssessment
                  do iDataSourceType = 0, size(c_DataSource) - 1
                     DataSourceType = c_DataSource(iDataSourceType)
                     NumDataSources = GetNumObservedDataElms(Opt, DataSourceType)
                     if (NumDataSources > 0) then
                        do iDataSource = 0, NumDataSources - 1
                           pDataSource => GetDataSource(Opt, DataSourceType, iDataSource)
                           pDataSource%ObjType = ObjT
                        enddo
                     endif
                  enddo
                  do iYear = 0, NumCatchYears(Opt%IndexInput) - 2
                     pParamElm => GetParamElm(Opt, cNValues, 0, iYear, &
                     NumTrueAges(Opt%IndexInput) - 1, .false.)
                     pParamElm%Flag = AFlag
                  enddo
                  call InitializeActiveParameters(Opt)
                  call DoRun(Opt)
                  open(cFileUnit, file = FileName, action = 'write', position =  'append')
                  write(cFileUnit, '(5I2,1000F14.5)') Model, ISVPAType, PlusGroup, ObjT, AFlag, &
                     Opt%Loc%ObjFunc, Opt%PopModel%StockSummary(0 : NumYears(Opt%IndexInput) - 1, ssSSB)
                  close(cFileUnit);
                  call CloseRun(Opt)
               enddo
            enddo
         enddo
      enddo
   enddo
end subroutine DoTestingOfAllRuns
